package com.zw.controller.admin.tools;


import com.zw.annotation.Log;
import com.zw.core.controller.BaseController;
import com.zw.core.domain.AjaxResult;
import com.zw.core.page.TableDataInfo;
import com.zw.entity.Project;
import com.zw.enumeration.BusinessType;
import com.zw.service.ProjectService;
import com.zw.utils.poi.ExcelUtil;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 项目管理
 *
 * @author zw
 */
@RestController
@RequestMapping(("/tools/project"))
public class PtProjectController extends BaseController {

    @Resource
    private ProjectService projectService;

    /**
     * 查询项目列表
     */
    @PreAuthorize("@ss.hasPermi('tools:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(Project project) {
        startPage();
        List<Project> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    /**
     * 查询项目详细
     *
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('tools:project:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id) {
        return AjaxResult.success(projectService.selectProjectById(id));
    }

    /**
     * 新增项目
     */
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('tools:project:add')")
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Project project) {
        return toAjax(projectService.save(project));
    }

    /**
     * 修改项目
     */
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('tools:project:edit')")
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Project project) {
        return toAjax(projectService.updateById(project));
    }

    /**
     * 删除项目
     */
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('tools:project:remove')")
    @DeleteMapping("/{projectIds}")
    public AjaxResult remove(@PathVariable Long[] projectIds) {
        return toAjax(projectService.removeBatchByIds(Arrays.asList(projectIds)));
    }

    /**
     * 导出项目
     */
    @Log(title = "项目管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('tools:project:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, Project project) {
        List<Project> projects = projectService.selectProjectList(project);
        ExcelUtil<Project> util = new ExcelUtil<>(Project.class);
        util.exportExcel(response, projects, "项目列表");
    }
}
